<?php

namespace Admin\Controller;
//use Think\Controller;
use Tools\AdminController;

/**
* 商品列表
*/
class GoodsController extends AdminController
{
	
	function showlist1()
	{

		//$goods=new \Model\GoodsModel();
		//var_dump($goods);
		
		$goods=D('goods');
		var_dump($goods);

		$this->display();
	}

	function showlist2()
	{
		$goods=new \Model\GoodsModel();
		$info=$goods->select();
		$info=$goods->select(4000);
		

		$this->assign('info',$info);

		$this->display();
	}

	function showlist()
	{
		//实现数据分页
		$goods=D('goods');
		//获得总记录条数
		$total=$goods->count();
		$per=7;
		//实例化
		$page_obj=new \Tools\Page($total,$per);
		//自定义SQL
		$sql="select * from sw_goods ORDER BY goods_id DESC ".$page_obj->limit;
		$info=$goods->query($sql);

		//获得页码列表
		$pagelist=$page_obj->fpage();

		//$goods->where('goods_price>1000 and goods_name like "诺%"');
		//$goods->order('goods_price');

		//$goods->group('goods_brand_id');
		//$goods->field('goods_brand_id,max(goods_price)');

		$this->assign('pagelist',$pagelist);
		$this->assign('info',$info);
		$this->display();
	}

	function tianjia()
	{
		//$goods=new \Model\GoodsModel();

		//①数组add方式
		// $arr = array(
		// 	'goods_name' => 'samsung6s',
		// 	'goods_price' => '6000',
		// 	 'goods_weight' => '130',
		// 	 'goods_number' => '16',
		// 	);

		// $z=$goods->add($arr);
		// dump($z);

		//②AR方式
		// $goods->goods_name='nokia345';
		// $goods->goods_price='2222';
		// $goods->goods_weight='22';
		// $z=$goods->add();
		// dump($z);

		$goods=D('goods');

		//两个逻辑：展示表单，收集数据
		if(!empty($_POST)){
			//处理上传图片附件
			if($_FILES['goods_pic']['error']<4){
				$cfg=array(
					'rootPath'      =>  './Public/Uploads/', //保存根路径	
				);
				$up=new \Think\Upload($cfg);
				$z=$up->uploadOne($_FILES['goods_pic']);
//				dump($z);
//				exit;

				$bigimg=$up->rootPath.$z['savepath'].$z['savename'];
				$smallimg=$up->rootPath.$z['savepath'].'small_'.$z['savename'];

				//	上传好的图片制作缩略图
				$im=new \Think\Image();
				$im->open($bigimg);
				$im->thumb(100,100,6);
				$im->save($smallimg);		//保存缩略图到服务器


				//上传的图片传入数据库
				$_POST['goods_big_img']=ltrim($bigimg,'./');
				$_POST['goods_small_img']=ltrim($smallimg,'./');
			}

			//收集表单
			$z=$goods->add($_POST);
			if($z){
				//$this->redirect(地址，参数，跳转延迟时间，提示信息)；
				$this ->redirect('showlist',array('name'=>'tom','age'=>21),3,'ok');
			}else {
				$this ->redirect('tianjia',array('name'=>'tom','age'=>21),3,'false');
			}
		}else {
			//展示表单
			$this->display();
		}

		
	}

	function xiugai($goods_id)
	{
		// $goods=new \Model\GoodsModel();
		// $goods->goods_id='107';
		// $goods->goods_name='huawei111';
		// $goods->goods_price='2222';
		// $goods->goods_number='21';
		// $z=$goods->save();
		// dump($z);


		$goods=D('goods');

		if(!empty($_POST)){
			$z=$goods->save($_POST);
			if($z){
				$this ->redirect('showlist',array(),3,'ok');
			}else {
				$this ->redirect('xiugai',array('goods_id'=>$goods_id),3,'false');
			}
		}else{



		$info=$goods->find($goods_id);  //find()方法只返回一条记录并为一维数组
		$this->assign('info',$info);
		$this->display();
	}
	}
}